package chunmap.example.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import chunmap.data.provider.LayerFactory;
import chunmap.model.crs.CoordinateRSystem;
import chunmap.model.crs.proj.Mercator;
import chunmap.model.crs.proj.ReProjOp;
import chunmap.service.Service;
import chunmap.service.ServiceFactory;
import chunmap.service.gmap.GMService;
import chunmap.util.Logger;
import chunmap.view.layer.Layer;
import chunmap.view.layer.VectorLayer;
import chunmap.view.render.GeneralSymbol;
import chunmap.view.render.LabelSymbol;
import chunmap.view.render.Symbol;

public class GoogleMapService  extends HttpServlet{
	private static final long serialVersionUID = 1620311752296047028L;
	private static final Logger Log = new Logger(Logger.Debug,GoogleMapService.class.getName());

	@Override
	public void init(ServletConfig config) throws ServletException {
		Log.log(Logger.Info, "chunmap's googlemap service starting");
		super.init(config);

		List<Layer> layers=new ArrayList<Layer>();
		Layer layer = loadLayerData();
		layers.add(layer);

		Service gms = new GMService(layers);
		ServiceFactory.putService(gms, "GoogleMap");

		Log.log(Logger.Info, "chunmap's googlemap服务已启动");
	}

	private Layer loadLayerData() {
		String path1="D:\\Temp\\mapdata\\cntry02\\cntry02.shp";
		VectorLayer layer =  LayerFactory.openShapeFile(path1,1);
		layer.setDefaultStyle(new Symbol[]{new GeneralSymbol(),new LabelSymbol()});
		
		//reproj
		CoordinateRSystem target = new CoordinateRSystem();
        target.setProjection(new Mercator());
		ReProjOp.changeCRS(layer.getFeatures(), target);
		
		return layer;
	}

	// 请求地址参数
	// http://localhost:8080/chunmapService/test?SERVICE=GoogleMap&x=1&y=1&z=1

	public static void main(String[] args) {
		GoogleMapService ser = new GoogleMapService();

		try {
			ser.init(null);
		} catch (ServletException e) {
			e.printStackTrace();
		}
		System.out.println("ok");

	}
}
